//
//  ReduceViewController.swift
//  泛型介绍
//
//  Created by 崔博 on 2017/7/13.
//  Copyright © 2017年 崔博. All rights reserved.
//

import UIKit

class ReduceViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let a = sum(xs: [1,2,3])
        let array = [1,2,3]
        let paris = City(name: "Paris", population: 2241)
        let china = City(name: "china", population: 10000)
        let japan = City(name: "japan", population: 1)
        let russia = City(name: "russia", population: 312)
        
        let citier = [paris,china,japan,russia]

    }

    // 需求计算一个数组中所有证书类型之和的函数
    func sum(xs: [Int]) -> Int {
        var result: Int = 0
        for x in xs {
            result += x
        }
        return result
    }
}
struct City {
    let name: String
    let population: Int
}
//extension Array{
//    
//    func reduce<T: Sequene>(initial: T, combine: (T, Element) -> T) -> T {
//        let result = initial
//        for x in initial {
//            result = combine(result, x)
//        }
//        return result
//    }
//    
//}
extension Array{
        func customReduce<Result>(initialResult: Result, nextPartialResult: (Result, Element) -> Result) -> Result {
            var x = initialResult
            for e in self {
                x = nextPartialResult(x, e)
            }
            return x
        }
}
extension City{
    func cityByScalingPopulation() -> City {
        return City(name: name, population: population * 1000)
    }
}

